package club.pinea.handler.login;

import club.pinea.annotations.Head;
import club.pinea.interfaces.TCPMessage;
import club.pinea.model.User;
import club.pinea.proto.BasicProto;
import club.pinea.proto.CommandProto;
import club.pinea.proto.UserProto;
import club.pinea.send.MessageSend;
import club.pinea.service.UserService;
import club.pinea.session.IOSession;
import club.pinea.utils.StringUtils;
import com.google.protobuf.InvalidProtocolBufferException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.UUID;

/**
 * @description: 创建用户handler处理类
 * @author: 华上进
 * @createTime: 2021/6/26 14:43
 */
@Head(CommandProto.Command.C_S_REGISTER_VALUE)
@Component
public class UserRegisterHandler implements TCPMessage<BasicProto.BaseMessage> {
    // 用户接口
    @Autowired
    private UserService loginUserService;

    @Override
    public void doMessage(BasicProto.BaseMessage msg, IOSession session) throws Exception {
        UserProto.RegisterRequest registerRequest = UserProto.RegisterRequest.parseFrom(msg.getBody());
        UserProto.RegisterResponse registerResponse = null;
        // 获取注册用户信息
        String name = registerRequest.getName();
        if (name != null && name.length() > 10) {
            registerResponse = UserProto.RegisterResponse.newBuilder().
                    setRes(MessageSend.errorRes("用户名过长！")).build();
            MessageSend.sendToUser(registerResponse, session);
            throw new IllegalArgumentException("用户名过长！");
        }
        if (loginUserService.verifyName(name)) {
            registerResponse = UserProto.RegisterResponse.newBuilder().
                    setRes(MessageSend.errorRes("用户名已经存在！")).build();
            MessageSend.sendToUser(registerResponse, session);
            throw new IllegalArgumentException("用户名已经存在！");
        }
        String beInvited = registerRequest.getBeInvited().toUpperCase();
        //判断邀请码是否存在
        if (loginUserService.userCount(beInvited) <= 0) {
            registerResponse = UserProto.RegisterResponse.newBuilder().
                    setRes(MessageSend.errorRes("您的邀请码不正确！")).build();
            MessageSend.sendToUser(registerResponse, session);
            throw new IllegalArgumentException("您的邀请码不正确！");
        }

        String pass = registerRequest.getPass();
        // 生成随机的验证码
        UUID uuid = UUID.randomUUID();
        String token = uuid.toString().replace("-", "");

        User u = new User();
        // 获取注册账号的ip地址
        u.setIp(session.getIp());
        // 注册信息的添加
        u.setUsername(name);
        u.setPassword(StringUtils.encode(pass + token));
        u.setToken(token);
        u.setBeInvited(beInvited);
        // 调用注册方法得到受影响的行数
        int line = loginUserService.userRegister(u);
        // 判断返回的受影响行数，如果行数为1，则注册成功；行数为0则注册失败。
        if (line > 0) {
            registerResponse = UserProto.RegisterResponse.newBuilder().
                    setRes(MessageSend.successRes("恭喜您！注册成功！")).build();
            MessageSend.sendToUser(registerResponse, session);
        } else {
            registerResponse = UserProto.RegisterResponse.newBuilder().
                    setRes(MessageSend.errorRes("注册失败，请重新注册！")).build();
            MessageSend.sendToUser(registerResponse, session);
        }

    }
}
